package com.tang.test;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 非批量发送消息
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建生成者对象
        DefaultMQProducer producer = new DefaultMQProducer("producer_group",true);

        //2.为生产者对象设置nameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.把我们的生产者直接启动起来
        producer.start();
        //4.创建消息，发送消息

        for (int i = 0; i < 10; i++) {
            Date date=new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatDate = simpleDateFormat.format(date);
            String info="message from batch-producer :"+i+" 消息发送时间："+formatDate;
            Message msg = new Message("test-topic", "testTag", "test-key",info.getBytes());
            producer.send(msg);
        }
        System.out.println("发送成功");
    }
}